package com.lottery.material.service.admin.circle.impl;

import com.lottery.material.dao.circle.CircleRuleConfigMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.pojo.circle.CircleRuleConfig;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.circle.CircleRuleConfigCustom;
import com.lottery.material.service.admin.circle.CircleRuleConfigService;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description：正式圈员申请规则配置服务接口实现类
 * Created by 朱正磊 on 2019-04-13 15:41
 */
@Service("adminCircleRuleConfigService")
public class CircleRuleConfigServiceImpl implements CircleRuleConfigService {

    private final CircleRuleConfigMapper circleRuleConfigMapper;

    @Autowired
    public CircleRuleConfigServiceImpl(CircleRuleConfigMapper circleRuleConfigMapper) {
        this.circleRuleConfigMapper = circleRuleConfigMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param circleRuleConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(CircleRuleConfig circleRuleConfig) {
        circleRuleConfig.setCreateTime(new Date());
        circleRuleConfig.setIsDelete((Integer) CommonEnum.IS_DELETE_FALSE.getValue()); //默认未逻辑删除
        return circleRuleConfigMapper.insertSelective(circleRuleConfig);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return circleRuleConfigMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public CircleRuleConfig findById(Integer id) {
        return circleRuleConfigMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param circleRuleConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(CircleRuleConfig circleRuleConfig) {
        return circleRuleConfigMapper.updateByPrimaryKeySelective(circleRuleConfig);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<CircleRuleConfigCustom> findCustomListPage(Page page) {
        List<CircleRuleConfigCustom> list = circleRuleConfigMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public CircleRuleConfigCustom findCustomById(Integer id) {
        CircleRuleConfigCustom custom = circleRuleConfigMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(CircleRuleConfigCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //是否删除（1：否；2：是）
        custom.setIsDeleteDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.IS_DELETE, custom.getIsDelete(), CommonEnum.class));
    }

    /**
     * description：查询没有被逻辑删除的数据
     * user 朱正磊
     * time 2019-05-15 14:21
     * @return 返回没有被逻辑删除的数据集合列表
     */
    @Override
    public List<CircleRuleConfig> selectNotBeDeleted() {
        return circleRuleConfigMapper.selectNotBeDeleted();
    }

}
